﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
using System.IO;
namespace Things.Web.CropsPlantProgressInfo
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    BindCropsPlantProgress();

                    Guid CropsPlantProgressInfoID = new Guid(Request.Params["id"]);
                    ShowInfo(CropsPlantProgressInfoID);
                }
            }
        }

        protected void BindCropsPlantProgress()
        {
            Things.BLL.CropsPlantProgress cppBLL = new BLL.CropsPlantProgress();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = cppBLL.GetList(-1, strWhere.ToString(), "PlantProgressOrder");
            Things.Common.UICommon.BindDDL(ddlPlantProgress, ds, "PlantProgressName", "PlantProgressID");
        }

        private void ShowInfo(Guid CropsPlantProgressInfoID)
        {
            Things.BLL.CropsPlantProgressInfo bll = new Things.BLL.CropsPlantProgressInfo();
            Things.Model.CropsPlantProgressInfo model = bll.GetModel(CropsPlantProgressInfoID);
            this.lblCropsPlantProgressInfoID.Text = model.CropsPlantProgressInfoID.ToString();
            this.lblCropsID.Text = model.CropsID.ToString();
            this.lblPlantProgressID.Text = model.PlantProgressID.ToString();

            Things.Common.UICommon.SetSelectedDDL(ddlPlantProgress, model.PlantProgressID.ToString());

            Session[Things.Common.ConstantString.Images] = model.Image;
            this.Image1.ImageUrl = "../DisplayImage.aspx";

            this.txtPhotoDatetime.Text = model.PhotoDatetime.Value.ToString("yyyy-MM-dd");
            this.txtCreateDatetime.Text = model.CreateDatetime.Value.ToString("yyyy-MM-dd");
            this.txtComments.Text = model.Comments;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (!PageValidate.IsDateTime(txtPhotoDatetime.Text))
            {
                strErr += "照片日期格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtCreateDatetime.Text))
            {
                strErr += "记录时间格式错误！\\n";
            }
            if (this.txtComments.Text.Trim().Length == 0)
            {
                strErr += "备注不能为空！\\n";
            }

            Guid cppID = Guid.Empty;
            if (Guid.TryParse(this.ddlPlantProgress.SelectedValue, out cppID) == false)
            {
                strErr += "请选择种植过程类型！\\n";
            }
            
            byte[] content = Things.Common.UICommon.GetImages(FileUpload1);

            if ( string.IsNullOrEmpty(FileUpload1.FileName)==false && content.Length <= 0)
            {
                strErr += "图片格式不正确！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid CropsPlantProgressInfoID = new Guid(this.lblCropsPlantProgressInfoID.Text);
            Guid PlantProgressID = cppID;
            byte[] Image = content;
            DateTime PhotoDatetime = DateTime.Parse(this.txtPhotoDatetime.Text);
            DateTime CreateDatetime = DateTime.Parse(this.txtCreateDatetime.Text);
            string Comments = this.txtComments.Text;


            Things.BLL.CropsPlantProgressInfo bll = new Things.BLL.CropsPlantProgressInfo();
            Things.Model.CropsPlantProgressInfo model = bll.GetModel(CropsPlantProgressInfoID);
            if (model != null)
            {
                model.PlantProgressID = PlantProgressID;
                if (Image.Length > 0)
                {
                    model.Image = Image;
                }
                model.PhotoDatetime = PhotoDatetime;
                model.CreateDatetime = CreateDatetime;
                model.Comments = Comments;

                bll.Update(model);
                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
